package coms.process;




import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.LoggerFactory;
import com.beans.AccountBean;
import com.beans.AdminUserBean;
import com.beans.CategoryBean;
import com.beans.ClaimBean;
import com.beans.CompanyBean;
import com.beans.EmpDeptOrProjectCostBean;
import com.beans.EmployeeBankAccount;
import com.beans.FormBean;
import com.beans.ORGBean;
import com.beans.PayPlanLineBean;
import com.beans.ProjectBean;
import com.beans.SubCategory;
import com.beans.VendorBean;
import com.crc.mam.BaseCase;
import TestCases.BaseTestCase;
import coms.AutoException;
import coms.Constants;
import frame.FrameWork;
import frame.InterTest;
import functions.ems_base_data.Account;
import functions.ems_base_data.Category;
import functions.ems_base_data.MdOu;
import functions.ems_base_data.Project;
import functions.ems_base_data.Vendor;
import functions.ems_claim.AccountDeptCost;
import functions.ems_claim.Claim;
import functions.ems_claim.ClaimLine;
import functions.ems_claim.ClaimPay;
import functions.ems_claim.ClaimProcess;

import functions.ems_process_service.Psc;
import functions.mams_rest.Org;

public class EmployeeLoanClaimProcess extends BaseCase implements InterTest{
	private org.slf4j.Logger logger = LoggerFactory.getLogger(EmployeeLoanClaimProcess.class);
	

    String lang;
	String claimId;
	String claimNo;
	ProjectBean project;
	FormBean form;
	AdminUserBean loginUserBean;
	String amount ;
	boolean isPassed = false;
	String categoryCode ;
	String categoryName;
	String applyCompany;
	ORGBean applyOrgBean;
    
	public String getCategoryName() {
      return categoryName;
    }


    public void setCategoryName(String categoryName) {
      this.categoryName = categoryName;
    }


  public String getCategoryCode() {
    return categoryCode;
  }


  public void setCategoryCode(String categoryCode) {
    this.categoryCode = categoryCode;
  }



  public String getAmount() {
    return amount;
  }


  public void setAmount(String amount) {
    this.amount = amount;
  }


  public boolean isPassed() {
    return isPassed;
  }


  public void setPassed(boolean isPassed) {
    this.isPassed = isPassed;
  }


  public String getClaimId() {
		return claimId;
	}


	public void setClaimId(String claimId) {
		this.claimId = claimId;
	}


	public String getClaimNo() {
		return claimNo;
	}


	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}
		

	
	public String getApplyCompany() {
    return applyCompany;
  }


  public void setApplyCompany(String applyCompany) {
    this.applyCompany = applyCompany;
  }


  public ORGBean getApplyOrgBean() {
    return applyOrgBean;
  }


  public void setApplyOrgBean(ORGBean applyOrg) {
    this.applyOrgBean = applyOrg;
  }


	/**
     * 由于不同环境的业务大类code不同，所以必须填其一，categoryCode或者categoryName
     */
	@Override
	public void run(Map<Object, Object> paramMap) {
	  /**
       * 
       * 
       */
	  logger.info("员工借款---报账单申请--- ");
	  String baseUrl = (String) paramMap.get("baseUrl");
      if(baseUrl !=null) {
        FrameWork.setBaseUrl(baseUrl);
      }
	  LoginTest loginTest = new LoginTest();
      loginTest.run(paramMap);
      if(!loginTest.status) {
        remark = loginTest.remark;
        status = false;
        throw new AutoException("登录失败，阻碍测试");
      }
      Psc psc = new Psc();
      CategoryBean categoryBean = null ;
      categoryCode = (String) paramMap.get("categoryCode");
      categoryName = (String) paramMap.get("categoryName");
      amount = (String)paramMap.get("amount");
      if(amount == null) {
        amount = "1000";
      }
      logger.info(categoryCode);
      if(categoryName != null) {
        Category category = new Category();
        categoryBean = category.getCategoryByName(categoryName);
        categoryCode = categoryBean.getCategoryCode();
        if(categoryBean == null) {
          throw new AutoException("找不到对应的业务大类：" +categoryName+ ", 请确认后重新提交");
        }
      }
    
      psc.selectFlowParam(categoryCode);
      FormBean form = psc.getForm();
      Claim claim = new Claim();
      AdminUserBean userBean = claim.findCurrentEmployee();
      claim.setLoginUserBean(userBean);
      Org org = new Org();
      applyCompany = userBean.getCompanyDesc();
      applyOrgBean = org.getRandomOrg(applyCompany, userBean.getDepartment());
      Vendor vendor = new Vendor();
      EmployeeBankAccount accountBean = vendor.findForEmployee(userBean.getEmpNo());
      String buCode = userBean.getFinanceBusinessUnit();
      Account account = new Account();
      AccountBean accoutBean = account.getRandomAccountBean(buCode);
      MdOu ou = new MdOu();
      ou.getCompanyByCompanyName(applyCompany);
      CompanyBean ouCompany = ou.getMyCompany();
      logger.info("categoryBean： " +categoryBean.toString());
      claim.saveEmployeeLoanClaimHeadAndLine(categoryBean,userBean,accountBean,accoutBean,ouCompany);
      if(!claim.status) {
        status = false;
        remark = claim.remark;
        throw new AutoException("表单头保存失败，无法继续进行测试");
      }
      claimId = claim.getClaimId();
      claimNo = claim.getClaimNo();
      lang = claim.getLang();
    
      ClaimLine line = new ClaimLine();
      Category category = new Category();
      category.subcategory(categoryCode);
      ArrayList<SubCategory> subCategories = category.getSubCategories();
      SubCategory subCategory = subCategories.get(0);
      line.insert(claimId);
      if(!line.status){
        status = false;
        remark = line.remark;
        throw new AutoException("插入报账明细失败，无法继续进行测试");
      }
    
    int lineId = line.findList(claimId);
      if(!line.status){
        status = false;
        remark = line.remark;
        throw new AutoException("插入报账明细失败，无法继续进行测试");
      }
      AccountBean lineAccountBean = account.findOneLenderAccount(buCode,categoryCode ,"all",ouCompany.getCompanycode());
      if(lineAccountBean == null) {
        lineAccountBean = account.getRandomAccountBean(buCode);
      }
      logger.info(lineAccountBean.toString());
      Project project = new Project();
      project.findPagedProjectList(ouCompany.getCompanycode());
      ProjectBean selectProject = project.getSelectProject();      
      AccountDeptCost accountDeptCost = new AccountDeptCost();
      EmpDeptOrProjectCostBean empDeptCostBean = accountDeptCost.queryEmpDeptCostByApplOrgDept(userBean.getDepartmentId());
      line.saveRows(lang,String.valueOf(lineId),claimId,userBean, empDeptCostBean, lineAccountBean,selectProject,amount,subCategory );
      if(!line.status) {
        status = false;
        remark = line.remark;
        throw new AutoException("报账明细失败，无法继续进行测试，原因： "+ remark);
      }
      ClaimPay pay = new ClaimPay();
      PayPlanLineBean payPlanLineBean = pay.queryClaimPayPlan(claimId);
      ClaimProcess process = new ClaimProcess();
      
      process.submitProcess(claimId,form);
     
      if(!process.status) {
        status = false;
        remark = process.remark;   
        logger.error("员工借款失败： " + remark);
      }else {
        status = true;
        remark = "[claimId="+claimId+",claimNum="+claimNo+",amount="+amount+"]";
      }      		
	}


  @Override
  public boolean getStatus() {
   
    return status;
  }
	
  @Override
  public String getRemark() {
    return remark;
  }
	

}
